package org.black.phoneUtility.sms.schedule.app.fragment;

import java.util.Calendar;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

public class SmsScheduleDateTimeDatePickerDialogFragment extends DialogFragment {

	public static final String TAG = SmsScheduleDateTimeDatePickerDialogFragment.class
			.getName();
	public static final String ARGUMENTS_YEAR = "year";
	public static final String ARGUMENTS_MONTH = "month";
	public static final String ARGUMENTS_DATE = "date";

	public interface SmsScheduleDateTimeDatePickerDialogInterface {
		public void onSmsScheduleDateTimeDatePickerDialogPositiveClick(
				int year, int monthOfYear, int dayOfMonth);

		public void onSmsScheduleDateTimeDatePickerDialogNegativeClick();
	}

	private SmsScheduleDateTimeDatePickerDialogInterface smsScheduleDateTimeDatePickerDialogFragmentInterface;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsScheduleDateTimeDatePickerDialogFragmentInterface = (SmsScheduleDateTimeDatePickerDialogInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsScheduleDateTimeDatePickerDialogFragmentInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		Bundle bundle = getArguments();
		int year = bundle.getInt(ARGUMENTS_YEAR);
		int month = bundle.getInt(ARGUMENTS_MONTH);
		int date = bundle.getInt(ARGUMENTS_DATE);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_schedule_schedule_on_date_picker_dialog, null);
		final DatePicker smsScheduleDatePicker = (DatePicker) view
				.findViewById(R.id.smsScheduleScheduleOnrDatePicker);
		smsScheduleDatePicker.init(year, month, date, null);
		Button onTodayButton = (Button) view
				.findViewById(R.id.smsScheduleScheduleOnTodayButton);
		onTodayButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Calendar calendar = Calendar.getInstance();
				int year = calendar.get(Calendar.YEAR);
				int month = calendar.get(Calendar.MONTH);
				int date = calendar.get(Calendar.DATE);
				smsScheduleDatePicker.init(year, month, date, null);
			}
		});

		builder.setView(view)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								smsScheduleDateTimeDatePickerDialogFragmentInterface
										.onSmsScheduleDateTimeDatePickerDialogPositiveClick(
												smsScheduleDatePicker.getYear(),
												smsScheduleDatePicker
														.getMonth(),
												smsScheduleDatePicker
														.getDayOfMonth());

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
